Skip to content

Latest commit

 

History

History
42 lines (37 loc) · 1.04 KB

README.md

File metadata and controls

42 lines (37 loc) · 1.04 KB

67. Add Binary

Given two binary strings, return their sum (also a binary string).

The input strings are both non-empty and contains only characters 1 or 0.

Example 1:

Input: a = "11", b = "1" Output: "100" 

Example 2:

Input: a = "1010", b = "1011" Output: "10101" 

Solutions (Python)

1. Solution

classSolution: defaddBinary(self, a: str, b: str) ->str: i=-1c='0'ret=""whilei>=-len(a) ori>=-len(b): if (i<-len(a) ora[i] =='0') and (i<-len(b) orb[i] =='0'): ret=c+retc='0'elifi>=-len(a) anda[i] =='1'andi>=-len(b) andb[i] =='1': ret=c+retc='1'elifc=='0': ret='1'+retelse: ret='0'+reti-=1ifc=='1': ret='1'+retreturnret
close